package com.kintreda.ieco_server.module.account.service.impl;

import com.kintreda.ieco_server.bean.account.Role;
import com.kintreda.ieco_server.core.repository.IBaseRepository;
import com.kintreda.ieco_server.core.service.impl.BaseServiceImpl;
import com.kintreda.ieco_server.module.account.service.IRoleService;
import com.kintreda.ieco_server.util.Assert;
import com.kintreda.ieco_server.util.SQLUtils;
import com.kintreda.ieco_server.util.page.PageBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl<Role, String> implements IRoleService{

	@Autowired
	private IBaseRepository<Role, String> baseRepository;

	@Override
	public List<Role> getValidRoles(String  flatType){
		String hql = " from Role where status=1 ";
		if (StringUtils.isNotBlank(flatType)) {
			hql += "and flatType = '"+flatType+"'";
		}
		return super.query(hql, null);
	}

	@Override
	public Role getRoleByNameZh(String nameZh){
		String hql = " select t from Role t where t.nameEn=?";
		List<Role> list = this.baseRepository.findList(hql, new Object[] {nameZh});

		return Assert.notEmpty(list) ? list.get(0) : null;
	}

	@Override
	public List<Role> getRoleByategory(String type) {
		List<Role> list = query("from Role t where t.category=?", new Object[] {type});
		return Assert.notEmpty(list) ? list : null;
	}

	/******************************************************
	 * 获取角色信息
	 * @param roleCode
	 * @return
	 */
	public Role getRoleByCode(String roleCode) {

		String hql = " select t from Role t where t.nameEn=?";
		List<Role> list = this.baseRepository.findList(hql, new Object[] {roleCode});

		return Assert.notEmpty(list) ? list.get(0) : null;
	}

	/******************************************************
	 * 删除角色已经分配的菜单信息
	 * @param roleId
	 */
	public void deleteRoleMenus(String roleId) {

		String sql = " delete from common_role_menu where roleId=?";
		this.baseRepository.excuteBySql(sql, new Object[] {roleId});
	}

	@Override
	public PageBean<Role> query(PageBean<Role> page, Role role) {


		List<Object> params = new ArrayList<>();
		StringBuffer queryHql = new StringBuffer();
		boolean flag = true;

		queryHql.append("from Role role");
		if(StringUtils.isNotBlank(role.getNameZh())){
			flag = SQLUtils.append(queryHql, flag);
			queryHql.append(" role.nameZh like ? ");
			params.add("%"+role.getNameZh()+"%");
		}
		if(StringUtils.isNotBlank(role.getNameEn())){
			flag = SQLUtils.append(queryHql, flag);
			queryHql.append(" role..nameEn like ? ");
			params.add("%" + role.getNameEn()+"%");
		}

		if(StringUtils.isNotBlank(role.getStatus())){
			flag = SQLUtils.append(queryHql, flag);
			queryHql.append(" role.status = ? ");
			params.add(role.getStatus());
		}

		return super.queryPage(page,queryHql.toString(), " order by role.id desc ", params.toArray());




	}

	@Override
	public List<Role> getByNameZh(String unitOrPolice) {
		String hql="from Role where nameZh='"+unitOrPolice+"'";
		return super.query(hql, null);
	}

	@Override
	public Role getRoleByNameEh(String nameEh) {
		String hql="from Role where nameEh= ? ";
		List<Role> query = query(hql, new Object[]{nameEh});
		return query.size()>0?query.get(0):null;
	}
}
